﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Reflection.Metadata;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using Quartz.Impl;
using Quartz;
using Quartz.Logging;
using Quartz.Impl.Matchers;
using Common;

namespace ConsoleApp1
{
    public partial class ScheduleTask
    { 
        public static string[] Args { get; set; }
        public static bool InRunning { get; set; } = false;
        public static async Task RunTask(string[] args,int intervalMinutes)
        {
            Args = args;
#if DEBUG
            intervalMinutes = 12;
#endif
            DateTime TaskStartDate = DateTime.Now;

            try
            {
                NameValueCollection props = new NameValueCollection
                {
                    { "quartz.serializer.type", "binary" }
                };
                StdSchedulerFactory factory = new StdSchedulerFactory(props);
                IScheduler scheduler = await factory.GetScheduler();

                await scheduler.Start();

                #region LOGTEST
                IJobDetail job = JobBuilder.Create<ScheduleJob>()
                    .WithIdentity("Job1_ScheduleTask", "Group_ScheduleTask")
                    .Build();
                ITrigger trigger = TriggerBuilder.Create()
                    .WithIdentity("trigger1_ScheduleTask")
                    .StartAt(TaskStartDate)
                    .ForJob("Job1_ScheduleTask", "Group_ScheduleTask")
                    .WithCalendarIntervalSchedule(w => w.WithInterval(intervalMinutes,IntervalUnit.Minute)
                    )
                    .Build();

                await scheduler.ScheduleJob(job, trigger);
                await Task.Delay(TimeSpan.FromSeconds(0));
                #endregion LOGTEST 


                #region FileFlush
                //================================
                IJobDetail jobFileFlush = JobBuilder.Create<ScheduleFileFlushJob>()
                   .WithIdentity("JobFileFlush", "Group_ScheduleTask")
                   .Build();

                int IntervalDayTodo = 2;
                ITrigger triggerFileFlush = TriggerBuilder.Create()
                 .WithIdentity("triggerFileFlush")
                 .StartAt(TaskStartDate)
                 .ForJob("JobFileFlush", "Group_ScheduleTask").WithCalendarIntervalSchedule(w => w
                 .WithIntervalInMinutes(IntervalDayTodo)).Build();

                await scheduler.ScheduleJob(jobFileFlush, triggerFileFlush);

                #endregion

            }
            catch (SchedulerException se)
            {
                await Console.Error.WriteLineAsync(se.ToString());
            }
        }

        public class ScheduleJob : IJob
        {
            public async Task Execute(IJobExecutionContext context)
            {
                InRunning = true;
                string loggerLineJob = string.Format("[ScheduleJob] [INFO] [Execute::ScheduleTask:{0:yyyy-MM-dd HH:mm:ss fff}][{1}]", DateTime.Now, context.FireInstanceId);
                await Console.Out.WriteLineAsync(loggerLineJob); 
                CommonBase.OperateDateLoger(loggerLineJob);

                return;
            }
        }

        public class ScheduleFileFlushJob : IJob
        {
            public async Task Execute(IJobExecutionContext context)
            { 
                string loggerLineJob = string.Format("\n[{0:yyyy-MM-dd HH:mm:ss fff}] [INFO] [Execute::FileFlush.Run:{1}]", DateTime.Now, context.FireInstanceId);
                await Console.Out.WriteLineAsync(loggerLineJob);
                Common.CommonBase.OperateDateLoger(loggerLineJob);
                return;
            }
        }
    }
}
